import BaseView from "../../../Script/Base/BaseView";
import { EBundleType, EResType, EViewType } from "../../../Script/Common/Enum";
import { ISelectItem, IViewData } from "../../../Script/Interface/IMessage";
import { SceneGame } from "../../../Script/SceneGame";
import AppService from "../../../Script/Service/AppService";
import EventID from "../../Common/EventID";
import Message from "../../Common/Message";
import { TalkModule } from "../../Common/TalkModule";
import { AdManager } from "../../naodong/ads/AdManager";
import GameUtil from "../../Util/GameUtil";
import StorageUtil from "../../Util/StorageUtil";

const { ccclass, property } = cc._decorator;

export interface IMarketItem {
    id: number;
    itemId: number;
    name: string;
    num: number;
    sale: number;
}

const VIEW_DATA: IViewData = {
    name: 'SelectUI',
    type: EViewType.Panel,
    bundle: EBundleType.Game,
    zIndex: 100,
};

@ccclass
export default class SelectUI extends BaseView {
    @property({ type: cc.Node, tooltip: "成功按钮" })
    public btnSuccess: cc.Node = null;
    @property({ type: cc.Node, tooltip: "失败按钮" })
    public btnFail: cc.Node = null;
    @property({ type: cc.Label, tooltip: "描述" })
    public labDesc: cc.Label = null;

    protected onLoad() {
        super.onLoad();

        this.refreshUI();
    }

    private refreshUI() {
        if (this.prepareData.callback) {
            this.prepareData.callback();
            this.prepareData.callback = null;
        }

        if (this.prepareData.success || this.prepareData.fail) {
            this.btnSuccess.active = false;
            if (this.prepareData.success) {
                const video = cc.find("layout/video", this.btnSuccess);
                const labSucc = cc.find("layout/lab", this.btnSuccess).getComponent(cc.Label);
                labSucc.string = `${this.prepareData.success.lab}`;
                this.btnSuccess.active = true;
                video.active = this.prepareData.success.isVideo || false;
            }

            this.btnFail.active = false;
            if (this.prepareData.fail) {
                if (this.prepareData.fail.lab) {
                    const video = cc.find("layout/video", this.btnFail);
                    const labFail = cc.find("layout/lab", this.btnFail).getComponent(cc.Label);
                    labFail.string = `${this.prepareData.fail.lab}`;
                    this.btnFail.active = true;
                    video.active = this.prepareData.fail.isVideo || false;
                } else {
                    setTimeout(() => {
                        this.close();
                        this.prepareData?.fail?.callback();
                    }, 1500);
                }
            }
            this.labDesc.string = `${this.prepareData.desc}`;
        }
    }

    private async close() {
        SceneGame.instance.removePopView(this.node);
        await GameUtil.sleep(500);
        TalkModule.updateCheck();
    }

    private handler(data: ISelectItem) {
        if (data.isVideo) {
            // 播放视频
            AdManager.showVideoAd(() => {
                data.callback();
                this.close();
            });
        } else if (data.value && data.value > 0) {
            const gameData = StorageUtil.instance.getGameData();
            if (gameData.money >= data.value) {
                GameUtil.instance.changeResValue(EResType.Money, -data.value);
                Message.instance.send(EventID.REFRESH_TOP_BAR, { type: EResType.Money, value: -data.value });
                data.callback();
                this.close();
            } else {
                SceneGame.instance.showToast(cc.v2(0, 0), `资金不足`);
            }
        } else {
            data.callback();
            this.close();
        }
    }

    private async onBtnsClicked(event: cc.Event.EventTouch, eventData: string) {
        const name = event.target.name;
        switch (name) {
            case "btnSuccess":
                {

                    if (this.prepareData?.success?.callback) {
                        this.handler(this.prepareData?.success);
                    }
                }
                break;
            case "btnFail":
                {
                    if (this.prepareData?.fail?.callback) {
                        this.handler(this.prepareData?.fail);
                    } else {
                        this.prepareData = this.prepareData?.fail?.next;
                        this.refreshUI();
                    }
                }
                break;
        }
    }
}
AppService.instance.registViewData(VIEW_DATA);